#include "map.h"

using namespace std;

int Map::getHeight()
{
	return mS.m_height;
}

int Map::getWidth()
{
	return mS.m_width;
}

Map::Map()
{}

bool Map::loadMapFromFile(char *a_filename)
{
	ifstream is;
	is.open(a_filename);        // open file

	if(!is)
	{
		printf("unable to read file. \n");
		return false;
	}
	
	is >> mS.m_width >> mS.m_height;
	is.ignore(1024, '\n');

	char c = 0;
	//Allocating Memory for the 2dMap
		m_Map = new char *[mS.m_height];
		for(int i = 0;i < mS.m_height; ++i)
		{
			m_Map[i] = new char [mS.m_width];
			for(int j = 0; j < mS.m_width; ++j)
			{
				c = is.get();
				m_Map[i][j] = c;
			}
			is.ignore(1024, '\n'); //ignore the next 1024 char, or until after a '\n' char is read
		}
	is.close();           // close file
	return true; 
}

void Map::releaseMapFromFile()
{
	if(m_Map)
	{
		for(int i = 0; i < mS.m_height; ++i)
		{
			delete[] m_Map[i];
		}
		delete[] m_Map;
		mS.m_width = 0;
		mS.m_height = 0;
		m_Map = 0;
	}
}

Map::~Map()
{
	releaseMapFromFile();
}



